//
//  menuLayer.m
//  my-BTB
//
//  Created by QuickSoluion on 9/7/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "menuLayer.h"
#import "ChuGame.h"
#import  "XeKieu.h"
#import "SlideInRAnimation.h"
#import "gamePlayer.h"
@implementation menuLayer

-(id)init
{
	if( (self=[super init])) {
	
		bClickStartGame=TRUE;
		NSLog(@"abc");
		[self initBG];
		
		CCMenu *_menu=[CCMenu menuWithItems:nil];
		CCMenuItemImage *item=[CCMenuItemImage itemFromNormalImage:@"btStartGame.png" selectedImage:@"btStartGame_hover.png" disabledImage:@"btStartGame.png" target:self selector:@selector(playgame)]; 
	
		[_menu addChild:item];
		[_menu setAnchorPoint:ccp(0,0)];
		_menu.position=ccp(377,277);
		  
		[self addChild:_menu];
		_tempCharacter=[self initCharacterByClass:[XeKieu class]:ccp(40,240):1.0:NO];
		[self initCharacterByClass:[ChuGame class]:ccp(240,160):1.0:NO];
		
		[self clearCache];
	
	}
	
	return self;
}

- (void) clearCache
{
	[[CCTextureCache sharedTextureCache] removeUnusedTextures];
	[[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
}

-(void)changetoGame
{
	
	[[CCDirector sharedDirector] replaceScene:[SlideInRAnimation transitionWithDuration:2.0f scene:[GameControlLayer node]]];
}

-(void)playgame
{
	if(bClickStartGame==TRUE)
	{
		bClickStartGame=FALSE;
		NSMutableArray* actionList = [[NSMutableArray alloc] initWithCapacity:1];
	
		for (int i=0; i<25; i++) {
			[actionList addObject:[CCCallFunc actionWithTarget:self selector:@selector(MovingCar)]];
			[actionList addObject:[CCDelayTime actionWithDuration:(0.1f)]];
		}
		
		[actionList addObject:[CCCallFunc actionWithTarget:self selector:@selector(changetoGame)]];
		
		CCFiniteTimeAction* seq = [self getActionSequence:actionList];	
		[actionList release];			
		[self runAction:seq];
	}
}

-(void)MovingCar
{
	_tempCharacter.position=ccp(_tempCharacter.position.x+25,_tempCharacter.position.y);
	
}

-(void)initBG
{
	CCSprite *bgImage=[CCSprite spriteWithFile:@"bgMangHinhChinh.png"];
	[bgImage setAnchorPoint:ccp(0,0)];
	bgImage.position=ccp(0,0);
	[self addChild:bgImage];
	
}
-(Character*)initCharacterByClass:(Class)tempC:(CGPoint)p:(float)fScale:(BOOL)yes_noScale
{
	Character* _itemClass=[[[tempC alloc] initWithParent:self] autorelease];
	
	if(yes_noScale==YES)
	{
		[_itemClass setScale:fScale];
	}
	_itemClass.position = p	;				
	id Action = [_itemClass createAnimation];
	[_itemClass runAction:Action];
	
	return _itemClass;
	
}

-(CCFiniteTimeAction *) getActionSequence: (NSMutableArray *) actions
{
	CCFiniteTimeAction *seq = nil;
	for (CCFiniteTimeAction *anAction in actions)
	{
		if (!seq)
		{
			seq = anAction;
		}
		else
		{
			seq = [CCSequence actionOne:seq two:anAction];
		}
	}
	return seq ;
	
}
@end
